package com.springboot.kafka.kafka;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

/**
 * 监听服务器上的kafka是否有相关的消息发过来
 */
@Component
public class Producer {

    private final Log log = LogFactory.getLog(this.getClass());

    @Autowired(required = false)
    private KafkaTemplate<String, Object> kafkaTemplate;

    public ListenableFuture<SendResult<String, Object>> send(String topic, String key, String value) {
        log.info(String.format("<Producer> topic is %s, key is %s, value is %s ", topic, key, value));
        return kafkaTemplate.send(topic, key, value);
    }
}
